<?php

namespace app\admin\controller;

use app\common\model\LiveBannedAreaModel;
use app\common\model\LiveCategoryModel;

class BannedController extends BaseController
{
    public function index()
    {
        if ($this->request->isAjax()) {
            $model = LiveBannedAreaModel::field('*');
            if (($category_id = $this->get('category_id')) && is_numeric($category_id)) {
                $model = $model->where('category_id', $category_id);
            }

            return json([
                'count' => $model->count(),
                'list' => $model->order('id', 'desc')
                    ->page((int)$this->get('page', 1), (int)$this->get('limit', 20))
                    ->select()
                    ->toArray(),
                'code' => 0,
            ]);
        }

        return $this->assign([
            'categorys' => $gategorys = LiveCategoryModel::lists(),
            'categorys_json' => json_encode(array_column($gategorys, 'title', 'id'), JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES),
        ])->fetch();
    }


    public function add()
    {
        if ($this->request->isAjax()) {
            if (!in_array($category_id = $this->post('category_id'), array_column(LiveCategoryModel::lists(), 'id'))) {
                return static::bulidFail('非法类型');
            }

            if (!$nation = trim($this->post('nation', ''))) {
                return static::bulidFail('请选择国家');
            }

            if (mb_strlen($nation) > 14) {
                return static::bulidFail('国家长度过长 不能超过14个字符');
            }

            if (!$sheng = trim($this->post('sheng', ''))) {
                if ($nation == '中国') {
                    return static::bulidFail('请选择省份');
                }
            }

            if (!$shi = trim($this->post('shi', ''))) {
                if ($nation == '中国') {
                    //return static::bulidFail('请选择市区');
                }
            }

            if($sheng == '黑龙江') {
                $sheng = '黑龙';
            }

            if ($nation != '中国') {
                $sheng = $nation;
            }

            if (LiveBannedAreaModel::field('id')
                ->where('category_id', $category_id)
                ->where('province', $sheng)
                ->where('city', $shi)
                ->limit(1)->select()->toArray()) {
                return static::bulidFail('禁止观看的地区已存在, 请勿重复添加');
            }

            $model = new LiveBannedAreaModel();
            $model->category_id = $category_id;
            $model->province = $sheng;
            $model->city = $shi;

            if (!$model->save()) {
                return static::bulidFail();
            }
            // 记录日志
            $log_content = "添加禁播地区，ID：{$model->id}，地区：{$model->province} {$model->city}";
            parent::addAdminLog($log_content);

            return static::bulidSuccess();
        }
        return $this->assign([
            'categorys' => LiveCategoryModel::lists(),
        ])->fetch();
    }

    public function del()
    {
        if (!($id = trim($this->post('id', ''))) || !is_numeric($id)) {
            return static::bulidFail();
        }

        if (!$model = LiveBannedAreaModel::field('id,province,city')->where('id', $id)->find()) {
            return static::bulidFail();
        }

        if (!$model->delete()) {
            return static::bulidFail();
        }

        // 记录日志
        $log_content = "删除禁播地区，ID：{$model->id}，地区：{$model->province} {$model->city}";
        parent::addAdminLog($log_content);

        return static::bulidSuccess();
    }
}